const BASE_URL = 'https://huntwolf.top'
// const BASE_URL = 'http://192.168.205.31:8988'


//请求次数
var reqTimes = 0

const request = {
	get: (url, data) => {

		console.log('调用get方法...')
		/*加载弹框*/
		uni.showLoading({
			title: '请稍候...',
			mask: true
		});
		// uni.$emit('apiShowLoading')
		return new Promise((resolve,reject)=>{
			reqTimes++
			uni.request({
				url:BASE_URL+url,
				data:data,
				header:{
					//请求头需要token时再放
					'Accept': 'application/json',
					'Content-Type': 'application/x-www-form-urlencoded',
					'token':uni.getStorageSync('userLoginRes')?.token || ''
				},
				method:'GET',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					reject(err)
				},
				complete: () => {
					reqTimes--
					if(!reqTimes){
						uni.hideLoading()
						// uni.$emit('apiHideLoading')
					}
				}
			})
		})

	},
	post: (url, data) => {
		console.log('调用post方法...')

		/*加载弹框*/
		uni.showLoading({
			title: '请稍候...',
			mask: true
		});
		uni.$emit('apiShowLoading')
		return new Promise((resolve,reject)=>{
			reqTimes++
			uni.request({
				url:BASE_URL+url,
				data:data,
				header:{
					//请求头需要token时再放
					'Accept': 'application/json',
					'Content-Type': 'application/json',
					'token':uni.getStorageSync('userLoginRes')?.token || ''

				},
				method:'POST',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					reject(err)
				},
				complete: () => {
					reqTimes--
					if(!reqTimes){
						uni.hideLoading()
						// uni.$emit('apiHideLoading')
						
					}
				}
			})
		})
	},
	put: (url,data)=>{
		console.log('调用put方法...')

		/*加载弹框*/
		// uni.showLoading({
		// 	title: '请稍候...',
		// 	mask: true
		// });
		uni.$emit('apiShowLoading')
		return new Promise((resolve,reject)=>{
			reqTimes++
			uni.request({
				url:BASE_URL+url,
				data:data,
				header:{
					//请求头需要token时再放
					'Accept': 'application/json',
					'Content-Type': 'application/json',
					'token':uni.getStorageSync('userLoginRes')?.token || ''

				},
				method:'PUT',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					reject(err)
				},
				complete: () => {
					reqTimes--
					if(!reqTimes){
						// uni.hideLoading()
						uni.$emit('apiHideLoading')
						
					}
				}
			})
		})
	},

	delete: (url,data)=>{
		console.log('调用delete方法...')

		/*加载弹框*/
		// uni.showLoading({
		// 	title: '请稍候...',
		// 	mask: true
		// });
		uni.$emit('apiShowLoading')
		

		return new Promise((resolve,reject)=>{
			reqTimes++
			uni.request({
				url:BASE_URL+url,
				data:data,
				header:{
					//请求头需要token时再放
					'Accept': 'application/json',
					'Content-Type': 'application/json',
					'token':uni.getStorageSync('userLoginRes')?.token || ''

				},
				method:'DELETE',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					reject(err)
				},
				complete: () => {
					reqTimes--
					if(!reqTimes){
						// uni.hideLoading()
						uni.$emit('apiHideLoading')
						
					}
				}
			})
		})
	}
}

export default request
